package com.luntan.service.impl;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Date;

import com.luntan.dao.BaseDAO;
import com.luntan.service.BaseService;
import com.luntan.util.web.DateUtil;

/**
 * 业务层基础实现类,实现公用方法
 * 
 * @author MaShiQiong
 * @date 2016-11-14 14:14:27
 */
public class BaseServiceImpl<T, K extends Serializable> implements BaseService<T, K> {

	// 数据访问层
	protected BaseDAO<T, K> baseDAO;

	/**
	 * 设置一个数据访问层
	 * 
	 * @param baseDAO
	 */
	@Override
	public void setBaseDAO(BaseDAO<T, K> baseDAO) {
		this.baseDAO = baseDAO;
	}

	/**
	 * 构造查询语句中的WHERE部分
	 * 
	 * @param m
	 * @return
	 */
	protected <M> String getWhere(M m) {
		StringBuilder hsql = new StringBuilder("");
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(m.getClass());
			// 获取所有属性
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (int i = 0; i < propertyDescriptors.length; i++) {
				Method writeMethod = propertyDescriptors[i].getWriteMethod();
				Method readMethod = propertyDescriptors[i].getReadMethod();

				if (writeMethod == null || readMethod == null) {
					continue;
				}

				// 属性名
				String name = propertyDescriptors[i].getName();

				// 获取方法的所有参数类型
				Class<?>[] parameterTypes = writeMethod.getParameterTypes();

				for (int j = 0; j < parameterTypes.length; j++) {
					if (parameterTypes[j] == null) {
						continue;
					}
					
					// 参数类型简写名
					String simpleName = parameterTypes[j].getSimpleName();
					switch (simpleName) {
						
						case "String": {// 字符串类型
							String str = (String) readMethod.invoke(m, (Object[]) null);
							str = str == null ? "" : ((String) str).trim();
							if (!"".equals(str)) {
								hsql.append(" AND m.").append(name).append(" LIKE '%").append(str).append("%' ");
							}
							break;
						}
	
						case "Date": {// 日期类型
							Date date = (Date) readMethod.invoke(m, (Object[]) null);
							if (date != null) {
								hsql.append(" AND m.").append(name).append(" LIKE '%").append(DateUtil.dateSimplyStr(date))
										.append("%' ");
							}
							break;
						}
						
						case "Long": {// 长整型
							Long long1 = (Long) readMethod.invoke(m, (Object[]) null);
							if (long1 != null) {
								hsql.append(" AND m.").append(name).append(" LIKE '%").append(long1).append("%' ");
							}
							break;
						}
						
						case "Integer": {// 整型
							Integer integer1 = (Integer) readMethod.invoke(m, (Object[]) null);
							if (integer1 != null) {
								hsql.append(" AND m.").append(name).append(" LIKE '%").append(integer1).append("%' ");
							}
							break;
						}

					}

				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return hsql.toString();
	}

	@Override
	public T getEntity(Class<T> clazz, K k) throws Exception {
		return this.baseDAO.getEntity(clazz, k);
	}

}
